﻿using System;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using SerbinAV.MobileSales.Sales.Entities;

namespace SerbinAV.MobileSales.Sales.DAL
{
    public class EntityReader
    {
        public static Visit ReadVisit(SqlCeDataReader dr)
        {
            Visit visit = new Visit();
            visit.IdVisit = dr.GetInt32(dr.GetOrdinal("IdVisit"));
            visit.DateVisit = dr.GetDateTime(dr.GetOrdinal("DateVisit"));
            visit.GpsData = dr.IsDBNull(dr.GetOrdinal("GpsData"))
                            ? string.Empty :
                            dr.GetString(dr.GetOrdinal("GpsData"));
            visit.TimeEndVisit = dr.IsDBNull(dr.GetOrdinal("TimeEndVisit"))
                                     ? (DateTime?)null
                                     : dr.GetDateTime(dr.GetOrdinal("TimeEndVisit"));
            visit.IsExport = dr.IsDBNull(dr.GetOrdinal("IsExport"))
                                 ? false
                                 : dr.GetBoolean(dr.GetOrdinal("IsExport"));
            visit.BuyPointId = dr.IsDBNull(dr.GetOrdinal("BuyPointId"))
                                   ? (int?) null
                                   : dr.GetInt32(dr.GetOrdinal("BuyPointId"));
            visit.IsOrder = dr.GetInt32(dr.GetOrdinal("IsOrder")) > 0;
            return visit;

        }

        public static BuyPoint ReadBuyPoint(SqlCeDataReader dr)
        {
            BuyPoint buyPoint = new BuyPoint();
            buyPoint.IdBuyPoint = dr.GetInt32(dr.GetOrdinal("IdBuyPoint"));
            buyPoint.NameBuyPoint = dr.IsDBNull(dr.GetOrdinal("NameBuyPoint"))
                                        ? string.Empty
                                        : dr.GetString(dr.GetOrdinal("NameBuyPoint"));
            buyPoint.Adress = dr.IsDBNull(dr.GetOrdinal("Adress"))
                                        ? string.Empty
                                        : dr.GetString(dr.GetOrdinal("Adress"));
            buyPoint.Debts = dr.IsDBNull(dr.GetOrdinal("Debts"))
                                 ? 0.00d
                                 : dr.GetDouble(dr.GetOrdinal("Debts"));
            buyPoint.BuyerId = dr.IsDBNull(dr.GetOrdinal("BuyerId"))
                                 ? (int?)null
                                 : dr.GetInt32(dr.GetOrdinal("BuyerId"));
            return buyPoint;

        }

        public static Order ReadOrder (SqlCeDataReader dr)
        {
                Order order;

                    order = new Order();
                    order.IdOrder = dr.GetInt32(dr.GetOrdinal("IdOrder"));
                    order.DateOrder = dr.GetDateTime(dr.GetOrdinal("DateOrder"));
                    order.SumOrder = dr.GetDouble(dr.GetOrdinal("SumOrder"));
                    order.NumberOrder = dr.GetString(dr.GetOrdinal("NumberOrder"));
                    order.VisitId = dr.GetInt32(dr.GetOrdinal("VisitId"));
                    //order.SumOrder = order.RecordOrders.Sum(r => r.Amount);
                    order.SkladId = dr.GetInt32(dr.GetOrdinal("SkladId"));
                    char typeD = dr.GetValue(dr.GetOrdinal("State")).ToString()[0];
                    switch (typeD)
                    {
                        case 'E':
                            {
                                order.IsReadOnly = true;
                                break;
                            }
                        case 'R':
                            {
                                order.IsReadOnly = false;
                                break;
                            }
                        case 'V':
                            {
                                order.IsReadOnly = true;
                                order.NotDelivery = true;
                                break;
                            }
                    }
            return order;
        }

        public static Sklad ReadSklad (SqlCeDataReader dr)
        {
            Sklad sklad = new Sklad();
            int idSklad = dr.GetOrdinal("IdSklad");
            int nameSklad = dr.GetOrdinal("NameSklad");
            sklad.IdSklad = dr.GetInt32(idSklad);
            sklad.NameSklad = dr.IsDBNull(nameSklad) ? string.Empty : dr.GetString(nameSklad);
            return sklad;
        }
    
        public static BuyPointScheduler ReadbuyPointScheduler (SqlCeDataReader dr)
        {
            BuyPointScheduler buyPointScheduler = new BuyPointScheduler();
            buyPointScheduler.Sunday = dr.IsDBNull(dr.GetOrdinal("Sunday"))
                                           ? false
                                           : dr.GetBoolean(dr.GetOrdinal("Sunday"));
            buyPointScheduler.Sunday = dr.IsDBNull(dr.GetOrdinal("Sunday"))
                                           ? false
                                           : dr.GetBoolean(dr.GetOrdinal("Sunday"));
            buyPointScheduler.Monday = dr.IsDBNull(dr.GetOrdinal("Monday"))
                                           ? false
                                           : dr.GetBoolean(dr.GetOrdinal("Monday"));
            buyPointScheduler.Thursday = dr.IsDBNull(dr.GetOrdinal("Thursday"))
                                           ? false
                                           : dr.GetBoolean(dr.GetOrdinal("Thursday"));
            buyPointScheduler.Wednesday = dr.IsDBNull(dr.GetOrdinal("Wednesday"))
                                           ? false
                                           : dr.GetBoolean(dr.GetOrdinal("Wednesday"));
            buyPointScheduler.Thursday = dr.IsDBNull(dr.GetOrdinal("Thursday"))
                                           ? false
                                           : dr.GetBoolean(dr.GetOrdinal("Thursday"));
            buyPointScheduler.Friday = dr.IsDBNull(dr.GetOrdinal("Friday"))
                                            ? false
                                            : dr.GetBoolean(dr.GetOrdinal("Friday"));
            buyPointScheduler.Saturday = dr.IsDBNull(dr.GetOrdinal("Saturday"))
                                           ? false
                                           : dr.GetBoolean(dr.GetOrdinal("Saturday"));
            return buyPointScheduler;
        }
    }
}
